package com.fantasy.dao.impl;

import com.fantasy.dao.BaseDao;
import com.fantasy.dao.FavoriteDao;
import com.fantasy.entity.Favorites;
import com.fantasy.entity.Video;

import java.util.List;

public class FavoriteDaopImpl extends BaseDao implements FavoriteDao {
    @Override
    public int insert(Favorites favorite) {
        String sql = "INSERT INTO `favorites`(`user_id`,`video_id`) VALUE(?,?)";
        return super.update(sql,favorite.getUserId(),favorite.getVideoId());
    }

    @Override
    public int delete(Long video_id) {
        String sql = "DELETE FROM `favorites` WHERE `video_id`=?";
        return super.update(sql,video_id);
    }

    @Override
    public Integer queryForPageTotalCount(int user_id) {
        String sql = "SELECT COUNT(1) FROM `video` WHERE `id` = ANY (SELECT `video_id` FROM `favorites` WHERE `user_id` = ?)";
        Number count = (Number)queryForSingleValue(sql,user_id);
        return count.intValue();
    }

    @Override
    public List<Video> queryForPageItems(int user_id, int begin, int pageSize) {
        String sql = "SELECT * FROM `video` WHERE `id` = ANY (SELECT `video_id` FROM `favorites` WHERE `user_id` = ?) LIMIT ?,?";
        return super.queryForList(Video.class,sql,user_id,begin,pageSize);
    }

    @Override
    public Favorites queryFavoritesByUserIdAndVideoId(Integer userId, Integer videoId) {
        String sql = "select * from `favorites` where  `user_id`= ? AND `video_id`=?";
        return queryForOne(Favorites.class,sql,userId,videoId);
    }
}
